package com.riddle.admin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.riddle.model.Category;
import com.riddle.model.User;
import com.riddle.service.ArticleService;
import com.riddle.service.BaseService;
import com.riddle.service.CategoryService;
import com.riddle.util.JsonUtil;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.QueryModel;

@Controller
@RequestMapping("/admin/category")
public class CategoryController extends BaseController<Category>  {

	@Resource
	private ArticleService articleService;
	
	@Resource
	private CategoryService categoryService;
	
	@RequestMapping()
	public String index() {
		return "admin/category/index";
	}
	
	@RequestMapping(value="/list", produces="application/json;charset=utf-8")
	@ResponseBody
	public String list(QueryModel queryModel) {
		List<Category> res = articleService.getCategories(queryModel.getQuery());
		return JsonUtil.toJson(res);
	}
	
	@RequestMapping("/add")
	public String add() {
		return "admin/category/add";
	}
	
	@RequestMapping(value="/addmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addModel(Category category) {
		return super.addModel(category);
	}
	
	@RequestMapping("/modify")
	public ModelAndView modify(Long id) {
		Category category = categoryService.query(id);
		return new ModelAndView("admin/category/modify", "category", category);
	}
	
	@RequestMapping(value="/modifymodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String modifyModel(Category category) {
		return super.modifyModel(category);
	}
	
	@RequestMapping(value="/delete", produces="application/json;charset=utf-8")
	@ResponseBody
	public String delete(Long id) {
		return super.delete(id);
	}

	@Override
	protected BaseService getService() {
		// TODO Auto-generated method stub
		return categoryService;
	}
}
